package com.coldworks.coldjoke.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.coldworks.base.BaseActivity;
import com.coldworks.base.manager.BaseConfManager;
import com.coldworks.coldjoke.R;
import com.coldworks.coldjoke.adapter.MenuAdapter;
import com.coldworks.coldjoke.adapter.MenuAdapter.MItem;
import com.coldworks.coldjoke.manager.UserManager;
import com.coldworks.coldjoke.manager.UserManager.UserStateSubject;
import com.coldworks.coldjoke.task.CheckVersionTask;
import com.umeng.fb.NotificationType;
import com.umeng.fb.UMFeedbackService;
import com.umeng.fb.util.FeedBackListener;

public class SettingActivity extends BaseActivity implements Observer {

	private List<Object> menuItems = new ArrayList<Object>();
	private MenuAdapter menuAdapter;
	private GridView menuGridView;
	private ImageButton backBtn;
	private UserStateSubject userStateSubject;
	private boolean isLogin = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try {
			setContentView(R.layout.activity_setting);
			userStateSubject = UserManager.getInstance().getUserStateSubject();
			userStateSubject.addObserver(this);
			isLogin = userStateSubject.getUserState() == UserManager.LOGINED ? true
					: false;
			initViews();
		} catch (Exception e) {
			log.error("", e);
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		try {
			UMFeedbackService.enableNewReplyNotification(context,
					NotificationType.NotificationBar);
			FeedBackListener listener = new FeedBackListener() {
				@Override
				public void onSubmitFB(Activity activity) {

					EditText contactText = (EditText) activity
							.findViewById(R.id.umeng_fb_contact_text);

					Map<String, String> contactMap = new HashMap<String, String>();
					contactMap.put("Contact", contactText.getText().toString());
					UMFeedbackService.setContactMap(contactMap);
					Map<String, String> remarkMap = new HashMap<String, String>();
					remarkMap.put("version", android.os.Build.VERSION.SDK + ","
							+ android.os.Build.VERSION.RELEASE);
					UMFeedbackService.setRemarkMap(remarkMap);

				}

				@Override
				public void onResetFB(Activity activity,
						Map<String, String> contactMap,
						Map<String, String> remarkMap) {
				}
			};
			UMFeedbackService.setFeedBackListener(listener);
			listener = null;
		} catch (Exception e) {
			log.error("", e);
		}
	}

	private void initViews() {
		backBtn = (ImageButton) findViewById(R.id.button_back);
		menuGridView = (GridView) findViewById(R.id.setting_gridview);
		if (isLogin) {
			menuItems.add(new MItem("退出登陆", 0));
		} else {
			menuItems.add(new MItem("登陆注册", 0));
		}
		menuItems.add(new MItem("检测新版", 0));
		menuItems.add(new MItem("关于", 0));
		menuItems.add(new MItem("反馈", 0));
		menuAdapter = new MenuAdapter(context, menuItems,
				R.layout.menu_row_setting_category,
				R.layout.menu_row_setting_item);
		menuGridView.setAdapter(menuAdapter);
		menuGridView.setOnItemClickListener(mItemClickListener);
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private OnItemClickListener mItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			switch (position) {
			case 0:// 登陆、注册、退出登陆
				if (isLogin) {
					isLogin = false;
					MItem item = new MItem("登陆注册", 0);
					menuItems.remove(0);
					menuItems.add(0, item);
					menuAdapter.notifyDataSetChanged();
					userStateSubject.setUserState(UserManager.UNLOGINED);
					UserManager.getInstance().setIsAutoLogin(false);
					log.info("" + isLogin);
				} else {
					startActivity(new Intent(SettingActivity.this,
							LoginActivity.class));
					log.info("" + isLogin);
				}
				break;
			case 1:// 检测新版本
				new CheckVersionTask(context, BaseConfManager.getInstance()
						.getVersionCode(context), false).execute();
				break;
			case 2:// 进入关于页面
				startActivity(new Intent(SettingActivity.this,
						AboutActivity.class));
				break;
			case 3:// 打开友盟反馈
				UMFeedbackService.setGoBackButtonVisible();
				UMFeedbackService.openUmengFeedbackSDK(SettingActivity.this);
				break;
			default:
				break;
			}

		}
	};

	/**
	 * 登陆成功接受广播，修改菜单所显示内容。
	 */
	@Override
	public void update(Observable o, Object data) {
		UserStateSubject observable = (UserStateSubject) o;
		if (observable.getUserState() == UserManager.LOGINED) {
			isLogin = true;
			MItem item = new MItem("退出登陆", 0);
			menuItems.remove(0);
			menuItems.add(0, item);
			menuAdapter.notifyDataSetChanged();
			log.info("login notify");
		} else {
			isLogin = false;
			MItem item = new MItem("登陆注册", 0);
			menuItems.remove(0);
			menuItems.add(0, item);
			menuAdapter.notifyDataSetChanged();
			log.info("logout notify");
		}

	}
}
